import { Component, OnInit , ViewChild} from '@angular/core';
import { CommonService } from '../../service/common.service'
import { StorageService } from "../../service/storage.service";

import { Router ,ActivatedRoute } from '@angular/router'; //导入router服务
import { EventService } from '../../service/event.service'


@Component({
  selector: 'app-login',
  templateUrl: './login.page.html',
  styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
  // debugger
  @ViewChild('iphone') myphone ;
  @ViewChild('iyzm') myyzm ;

  // 定义 输入内容
  public phone:string = ''
  public yzm:string = ''
  public flagYzm:boolean = true;
  public times:number = 60;
  public sessionId:any='';
  public deviceId:string='';
  public userInfo:any;
  public PersonalInfo:any;
  public begin:any;

  public show:boolean = false;
  public ischeck:boolean=false
  public isclick:boolean = false;
  

  constructor(private eventService:EventService,private common:CommonService,
    private storage:StorageService,private router:Router,private activatedRoute:ActivatedRoute) {
  }

  // 初始化
  ngOnInit() {
    if(this.storage.get('userInfo') !== undefined && this.storage.get('userInfo') !== null){
      console.log(this.storage.get('userInfo'))
      this.router.navigateByUrl('tabs/tab1')
    }
   
    this.flagYzm = true;
    this.times = 60
  } 

  ionViewDidEnter(){
    this.flagYzm = true;
    this.times = 60
    window.clearInterval(this.begin)
  }

  // 协议是否为真
  ischeckbox(){
    console.log(this.ischeck)
    this.isClick()
  }

  // 验证码
  getYzm(){
    // console.log(1111)
    // 校验手机
    if(this.common.yzShouJi(this.phone) == false){
      return false
    }
    // 传参数
    this.show = true
    var apis = '/tbr/sendMessageForLogin'
    var paramss = {'phone': this.phone}
    this.common.post(apis,paramss,true).then((response:any)=>{
      
      if(response.code == '200'){
        setTimeout(()=>{
          this.show=false
        },500)
        this.sessionId  = response.data.sessionId
        // console.log(response)
        this.times = 60;
        
        // 60s倒计时
        this.flagYzm = false
        this.begin = setInterval(()=>{   
            this.times--;
            if(this.times == 0){ 
                this.times = 60;
                this.flagYzm = true                     
                clearInterval(this.begin);
            }
        },1000)
      }else{
        this.common.presentAlert(response.msg)
        setTimeout(()=>{
          this.show=false
        },500)

      }
    })
  }

  //判断可点击
  isClick(){
    // console.log(this.myphone)
    if(this.phone.length == 11){
      this.myphone.nativeElement.blur()
    }
    if(this.yzm.length == 4){
      this.myyzm.nativeElement.blur()
    }
    if(this.phone !== '' && this.yzm !== '' && this.ischeck == true){
      this.isclick=true
    }else{
      this.isclick=false
    }
  }


  // 登录
  login(){
    // 校验手机
    if(this.common.yzShouJi(this.phone) == false){
      return false
    }
    this.show = true

     // 传参数
     var api = '/tbr/login'
     var params = {'phone': this.phone,'sessionId':this.sessionId,'code':this.yzm,'deviceId':this.common._IsDevice()}
    
     this.common.post(api,params,true).then((response:any)=>{
        // console.log(response)
        if(response.code == 200){
          setTimeout(()=>{
            this.show=false
            this.userInfo = response.data
            //1、保存用户信息   2、跳转首页 (根)
            this.storage.set('userInfo',response);   
            this.getInfo()
          },500)
        }else{
          setTimeout(()=>{
            // this.common.presentAlert(response.msg)
            this.show=false
            this.flagYzm = true;
            clearInterval(this.begin);
          })
        }
     })
  }

  // 传入id获取用户基本信息
  getInfo(){
    // 获取登录信息
    var api = '/tbr/getUserInfo'
    var params = {'userId': this.userInfo.id}
    
    this.common.post(api,params,false).then((response:any)=>{
      if(response.code == 200){
        this.show = false
        this.PersonalInfo = response.data
        // console.log("-----用户信息------")
        if(this.PersonalInfo.avater == '' || this.PersonalInfo.avater == null){
          this.PersonalInfo.avater = 'assets/avater.png'
        }
        // 保存信息
        this.storage.set('PersonalInfo',this.PersonalInfo)
        // this.eventService.event.emit('useraction')
        this.router.navigateByUrl("tabs/tab1")
      }else{
        this.common.presentAlert(response.msg)
        this.show=false
      }
    })
  }

}
